import uvicorn
from loguru import logger
import os
from api.routes import app
from config.settings import settings

# Configure logging
logger.add(
    "logs/app.log",
    rotation="500 MB",
    retention="10 days",
    level=settings.LOG_LEVEL,
    format=settings.LOG_FORMAT
)

def create_required_directories():
    """Create necessary directories for the application."""
    directories = [
        "logs",
        settings.PPT_OUTPUT_DIR
    ]
    for directory in directories:
        os.makedirs(directory, exist_ok=True)
        logger.info(f"Ensured directory exists: {directory}")

def main():
    """Main entry point of the application."""
    try:
        # Create required directories
        create_required_directories()

        # Log startup information
        logger.info("Starting Agent Service")
        logger.info(f"Environment: {'Development' if settings.DEBUG else 'Production'}")
        logger.info(f"API will be available at: http://{settings.API_HOST}:{settings.API_PORT}")
        
        # Check if DeepSeek API key is configured
        if not settings.DEEPSEEK_API_KEY:
            logger.warning("DeepSeek API key not configured. PPT generation will be unavailable.")

        # Start the server
        uvicorn.run(
            app,
            host=settings.API_HOST,
            port=settings.API_PORT,
            reload=settings.DEBUG
        )

    except Exception as e:
        logger.error(f"Failed to start the application: {str(e)}")
        raise

if __name__ == "__main__":
    main() 